import { defineStore } from 'pinia'
import { ref } from 'vue'

import { storesKey } from '@/config/stores'

export const useSystemStore = defineStore(
  storesKey.system,
  () => {
    const serviceAccess = ref<any>()
    const systemLogo = ref('')
    const systemIcon = ref('')
    const systemTitle = ref(import.meta.env.VITE_APP_TITLE)
    const websiteUrl = ref('')
    const captcha_admin_open = ref(true)
    //  function updateSystemInfo(data: WebsiteCOnfigReturn) {
    //    if (data.website?.manage_logo?.use) {
    //      systemLogo.value = data.website?.manage_logo?.path
    //        ? `${baseUrl.value}/${data.website?.manage_logo?.path}`
    //        : getAssetsUrl('modules/auth/logo.png')
    //    } else {
    //      systemLogo.value = data.website?.custom_logo?.manage
    //        ? `${baseUrl.value}/${data.website?.custom_logo?.manage}`
    //        : getAssetsUrl('modules/auth/logo.png')
    //    }
    //    systemTitle.value = data.website?.manage_logo?.title || 'CDN业务管理端'
    //  }
    function updateSystemInfo(data: any) {
      if (data.website?.logos?.length === 0) {
        systemLogo.value = getAssetsUrl('logo.png')
        systemIcon.value = getAssetsUrl('icon.png')
      } else {
        if (data.website?.use! < 0) {
          systemLogo.value = getAssetsUrl('logo.png')
          systemIcon.value = getAssetsUrl('icon.png')
        } else {
          const index = data.website?.use!
          systemLogo.value = baseUrl.value + data.website?.logos![index]!
          systemIcon.value = baseUrl.value + data.website?.logos![index]!
        }
      }

      localStorage.setItem('logo', systemIcon.value)
      captcha_admin_open.value = data.website?.captcha_admin_open!
      websiteUrl.value = data.website?.official_website!
      systemTitle.value = data.website?.name || import.meta.env.VITE_APP_TITLE
    }
    //服务接入数据
    function setServiceInfo(data: any): void {
      serviceAccess.value = data
    }
    return {
      systemLogo,
      systemTitle,
      websiteUrl,
      captcha_admin_open,
      serviceAccess,
      updateSystemInfo,
      setServiceInfo
    }
  },
  { persist: { storage: sessionStorage } }
)
